//查询所有成绩

const tbody = document.querySelector('tbody')
//发送请求获取后台数据所有学员分数
function getScore() {
    axios.get('/score/list').then(result => {
        if (result.data.code === 0) {
            const obj = result.data.data
            let html = ''
    
            for (const key in obj) {
                const tempHTML = obj[key].score.reduce((h, v,i) =>
                h + `<td class="score">${v}<input type='text' data-id='${key}' data-batch='${i+1}' /> </td>`, '')
    
                html += `
                    <tr>
                        <th scope="row">${key}</th>
                        <td>${obj[key].name}</td>
                        ${tempHTML}
                    </tr>
                    `
                
            }
            tbody.innerHTML = html
                //失去焦点事件，提交给后端修改数据
                const inputs = document.querySelectorAll('tbody input')
                inputs.forEach(v => {
                    v.addEventListener('blur', function () {
                        const data = {
                            "stu_id": v.dataset.id,
                            "batch": v.dataset.batch,
                            "score": v.value
                        }
                        axios.post('/score/entry',data).then(result=>{
                           if (result.data.code===0) {
                            toastr.success(result.data.message)
                            getScore()
                           }
                        })
                    })
                })
        }
    })
}
getScore()

//双击显示input文本框并显示对应分数
tbody.addEventListener('dblclick', function (e) {
    const dom = e.target
    if (dom.nodeName === 'TD' && dom.classList.contains('score')) {
        const input = dom.querySelector('input')
        input.style.display = 'block'
        input.value = dom.innerText
        input.focus()
    }

})
